@file:Suppress("NOTHING_TO_INLINE")

package com.hzw.core.resources

import android.content.Context
import android.content.res.Resources
import android.view.View
import androidx.annotation.AttrRes
import androidx.annotation.DimenRes
import androidx.fragment.app.Fragment
import com.hzw.core.ctx.appCtx

fun Float.dp2px(): Float {
    val scale = Resources.getSystem().displayMetrics.density
    return this * scale + 0.5f
}

fun Int.dp2px(): Int {
    val scale = Resources.getSystem().displayMetrics.density
    return (this * scale + 0.5f).toInt()
}

fun Float.px2dp(): Float {
    val scale = Resources.getSystem().displayMetrics.density
    return this / scale + 0.5f
}

fun Int.px2dp(): Int {
    val scale = Resources.getSystem().displayMetrics.density
    return (this / scale + 0.5f).toInt()
}

fun Float.sp2px(): Float {
    val scale = Resources.getSystem().displayMetrics.scaledDensity
    return this * scale + 0.5f
}

fun Int.sp2px(): Int {
    val scale = Resources.getSystem().displayMetrics.scaledDensity
    return (this * scale + 0.5f).toInt()
}

fun Float.px2sp(): Float {
    val scale = Resources.getSystem().displayMetrics.scaledDensity
    return this / scale + 0.5f
}

fun Int.px2sp(): Int {
    val scale = Resources.getSystem().displayMetrics.scaledDensity
    return (this / scale + 0.5f).toInt()
}


inline fun Context.dimen(@DimenRes dimenResId: Int): Float = resources.getDimension(dimenResId)
inline fun Fragment.dimen(@DimenRes dimenResId: Int) = context!!.dimen(dimenResId)
inline fun View.dimen(@DimenRes dimenResId: Int) = context.dimen(dimenResId)
inline fun appDimen(@DimenRes dimenResId: Int) = appCtx.dimen(dimenResId)

inline fun Context.dimenPxSize(@DimenRes dimenResId: Int): Int =
    resources.getDimensionPixelSize(dimenResId)

inline fun Fragment.dimenPxSize(@DimenRes dimenResId: Int) = context!!.dimenPxSize(dimenResId)
inline fun View.dimenPxSize(@DimenRes dimenResId: Int) = context.dimenPxSize(dimenResId)
inline fun appDimenPxSize(@DimenRes dimenResId: Int) = appCtx.dimenPxSize(dimenResId)

inline fun Context.dimenPxOffset(@DimenRes dimenResId: Int): Int =
    resources.getDimensionPixelOffset(dimenResId)

inline fun Fragment.dimenPxOffset(@DimenRes dimenResId: Int) = context!!.dimenPxOffset(dimenResId)
inline fun View.dimenPxOffset(@DimenRes dimenResId: Int) = context.dimenPxOffset(dimenResId)
inline fun appDimenPxOffset(@DimenRes dimenResId: Int) = appCtx.dimenPxOffset(dimenResId)

fun Context.styledDimenPxSize(@AttrRes attr: Int): Int = withStyledAttributes(attr) { getDimensionPixelSize(it, -1) }
inline fun Fragment.styledDimenPxSize(@AttrRes attr: Int) = context!!.styledDimenPxSize(attr)
inline fun View.styledDimenPxSize(@AttrRes attr: Int) = context.styledDimenPxSize(attr)
inline fun appStyledDimenPxSize(@AttrRes attr: Int) = appCtx.styledDimenPxSize(attr)

fun Context.styledDimenPxOffset(@AttrRes attr: Int): Int = withStyledAttributes(attr) { getDimensionPixelOffset(it, -1) }
inline fun Fragment.styledDimenPxOffset(@AttrRes attr: Int) = context!!.styledDimenPxOffset(attr)
inline fun View.styledDimenPxOffset(@AttrRes attr: Int) = context.styledDimenPxOffset(attr)
inline fun appStyledDimenPxOffset(@AttrRes attr: Int) = appCtx.styledDimenPxOffset(attr)
